const app = getApp()
const computedBehavior = require('miniprogram-computed')

Page({
  behaviors: [ computedBehavior ],
  data: {
    carStatus: 'ready',
    washDevice: {},
    deviceInterval: null,
    modeSheetShow: false,
    washModeList: [],
    selectMode: null,
    selectIndex: null
  },
  computed: {
    washStatus (data) {
      // 空载 carReady0 washIng0 washDone0
      // 停好车 carReady1 washIng0 washDone0
      // 洗车中carReady0 washIng1 washDone0
      // 洗车完成carReady0 washIng0 washDone1
      // 车还没走carReady1 washIng0 washDone0
      // 空载 carReady0 washIng0 washDone0

      if (data.washDevice.washdone) {

        return 'done'
      } else if (data.washDevice.washing) {
        return 'washing'
      } else if (data.washDevice.carready){
        return 'ready'
      } else {
        return 'free'
      }
    }
  },
  onLoad: function (options) {
    this.setData({
      deviceId: options.id || '338220100087'
    })
    if (options.poll) {
      this.setData({
        deviceInterval: setInterval(()=>{
          this.getWashDeviceState(this.data.deviceId)
        },1000)
      })
    }
    this.getWashDeviceState(this.data.deviceId)
    this.getWashModeList()
  },
  getWashDeviceState (id) {
    const that = this
    app.api.getWashDeviceState({deviceid: id}).then(res => {
      console.log(res)
      if (res.resultCode === 0) {
        this.setData({
          washDevice: res.data
        })
        if (res.data.carready) {
          clearInterval(this.data.deviceInterval)
        }
      }
    })
  },
  handleStopButtonClick () {
    app.api.stopWash({tradno: this.data.deviceid}).then(res => {
      console.log(res)
      if (!res.resultCode) {
        wx.showModal({
          title: '停止成功',
          content: '点击确定返回首页',
          confirm: '确定',
          showCancel: false,
          success (res) {
            if (res.confirm) {
              console.log('用户点击确定')
              wx.navigateBack({
                delta: 10
              })
            }
          }
        })
      }
    })
  },
  handleBackButtonClick () {
    wx.navigateBack({
      delta: 10
    })
  },
  handleModeButtonClick () {
    this.setData({
      modeSheetShow: true
    })
  },
  handleModeSheetCloseClick () {
    this.setData({
      modeSheetShow: false
    })
  },
  getWashModeList () {
    app.api.getWashMode().then(res => {
      console.log(res)
      if (!res.resultCode) {
        this.setData({
          washModeList: res.data
        })
      }
      // oldprice: 2
      // optionkey: "ordinarywash"
      // optionname: "普通洗"
      // price: 2
    })
  },
  handleModeItemClick (e) {
    const item = e.currentTarget.dataset.item
    const index = e.currentTarget.dataset.index
    this.setData({
      selectMode: item,
      selectIndex: index
    })
  },
  createOrder () {
    const params = {
      washid: wx.getStorageSync('washId') || 'wash16082093616781391',
      deviceid: wx.getStorageSync('deviceId') || '338220100087',
      washtype:  this.data.selectMode.optionkey
    }
    const that = this
    app.api.createOrder(params).then(res => {
      console.log(res)
      if (!res.resultCode) {
        that.payment(res.data.tradno)
      }
      // carnumber: ""
      // cellphone: "18638709009"
      // channel: null
      // createtime: 1611749144575
      // deviceid: "338220100087"
      // endstatus: 3
      // id: 67
      // money: 2
      // status: 0
      // tradno: "wash16117491445741974"
      // uid: "69"
      // updatetime: 1611749144575
      // washid: "wash16082093616781391"
      // washtype: "ordinarywash"
    })
  },
  payment (orderId) {
    app.api.pay({tradno: orderId, channel: 1}).then(res => {
      console.log(res)
      if (!res.resultCode) {
        // const payOptions = JSON.parse(res.data)
        const payOptions = res.data
        console.log(payOptions)
        wx.requestPayment({
          'timeStamp': payOptions.timeStamp,
          'nonceStr': payOptions.nonceStr,
          'package': payOptions._package,
          'signType': 'MD5',
          'paySign': payOptions.paySign,
          success(res) {
            console.log(res)
            wx.redirectTo({
              url: '/pages/paySuccess/paySuccess'
            })
          },
          fail(res) {
            console.log(res)
          }
        })
      }

    })
  }
});